package com.longrise.android.byjk.wxapi;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.app.BYJKAppLike;
import com.longrise.android.byjk.common.MessageConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.model.WXPaybean;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.ListenUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler, OnClickListener {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    private boolean isPaySuccess = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_wxpay);

        // api = WXAPIFactory.createWXAPI(this, UserInfor.getInstance().APP_ID);
        api = BYJKAppLike.mIWXAPI;
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            View mDialogview = View.inflate(WXPayEntryActivity.this, R.layout.dialog_courses_evaluate, null);
            TextView mTvtitle = (TextView) mDialogview.findViewById(R.id.dialog_courses_title);
            mTvtitle.setText("微信支付");
            ImageView mIv = (ImageView) mDialogview.findViewById(R.id.dialog_share_success_iv);
            TextView mTvcontent = (TextView) mDialogview.findViewById(R.id.dialog_courses_content);
            TextView mTvbt = (TextView) mDialogview.findViewById(R.id.dialog_courses_evaluate_bt);
            mTvbt.setOnClickListener(this);
            isPaySuccess = false;
            switch (resp.errCode) {
                case 0:
                    mTvcontent.setText("支付成功");
                    mIv.setImageResource(R.drawable.icon_right);
                    WXPaybean.getInstance().setSuccess(true);
                    UserInfor.getInstance().setToConfirm_wx(true);
                    ListenUtil.getInstance().setPay(2);
                    isPaySuccess = true;
                    break;
                case -1:
                    mTvcontent.setText("支付失败");
                    mIv.setImageResource(R.drawable.icon_tips);
                    WXPaybean.getInstance().setSuccess(false);
                    ListenUtil.getInstance().setPay(0);
                    break;
                case -2:
                    mTvcontent.setText("支付失败");
                    mIv.setImageResource(R.drawable.icon_tips);
                    WXPaybean.getInstance().setSuccess(false);
                    ListenUtil.getInstance().setPay(0);
                    break;

                default:
                    break;
            }

            DialogUtil.getInstance().creatAlertDialog(WXPayEntryActivity.this, mDialogview, 266, 195);


        }
    }

    /**
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_courses_evaluate_bt:
                DialogUtil.getInstance().dismiss();
                finish();
                if (isPaySuccess) {
                    Intent intent = new Intent(MessageConstants.WXPAY_SUCCESS);
                    sendBroadcast(intent);
                }
                overridePendingTransition(0, 0);
                break;

            default:
                break;
        }

    }

}